package com.shy.collect.controller;

import com.shy.collect.service.OperatorService;
import com.shy.configs.common.enums.RCode;
import com.shy.configs.mvc.models.AjaxResult;
import com.shy.configs.mvc.models.dto.OperatorLoginDto;
import com.shy.configs.mvc.models.menus.SubMenu;
import com.shy.configs.mvc.models.query.OperatorLoginQuery;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 登录操作
 */
@CrossOrigin
@RestController
@RequestMapping("/operator")
public class OperatorController {

    @Resource
    private OperatorService operatorService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody @Validated OperatorLoginQuery operatorLoginQuery) {
        // 调用service,实现登录
        OperatorLoginDto loginDto = operatorService.login(operatorLoginQuery);
        //封装为AjaxResult对象
        return AjaxResult.success(loginDto);
    }
    /**
     * 查询菜单
     */
    @GetMapping("/menus")
    public AjaxResult menu(@RequestHeader("loginId") Long operatorId) {
        if (operatorId!=null && operatorId>0){
            //调用service查询菜单
            List<SubMenu> menus =operatorService.queryMenuByOperatorId(operatorId);
            return AjaxResult.success(menus);
        }
        return AjaxResult.fail(RCode.REQUEST_PARAM_ERROR);
    }
    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public AjaxResult logout(@RequestHeader("loginId") Long operatorId) {
        if (operatorId!=null && operatorId>0){
            //调用service查询菜单
            boolean flag =operatorService.logout(operatorId);
            return flag?AjaxResult.success(): AjaxResult.fail(RCode.FAIL);
        }
        return AjaxResult.fail(RCode.REQUEST_PARAM_ERROR);
    }
}
